package com.zl.tby.service.impl;

import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyEcologyInfo;
import com.zl.tby.model.TbyEcologyReal;
import com.zl.tby.repository.TbyEcologyRealRepo;
import com.zl.tby.service.ITbyEcologyRealService;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.annotation.Db;

import java.time.LocalDateTime;

/**
 * 生态因子实时数据 接口实现类
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Slf4j
@Component
public class TbyEcologyRealServiceImpl implements ITbyEcologyRealService {

    @Db
    private TbyEcologyRealRepo tbyEcologyRealRepo;

    @Override
    @Tran
    public R<TbyEcologyReal> add(TbyEcologyInfo form) {
        TbyEcologyReal entity = new TbyEcologyReal();
        entity.setId(form.getStationId());
        entity.setTemperature(form.getTemperature());
        entity.setHumidity(form.getHumidity());
        entity.setRain(form.getRain());
        entity.setLux(form.getLux());
        entity.setOxygen(form.getOxygen());
        entity.setPmOne(form.getPmOne());
        entity.setPmTwo(form.getPmTwo());
        entity.setWindSpeed(form.getWindSpeed());
        entity.setWindDirection(form.getWindDirection());
        entity.setPressure(form.getPressure());
        entity.setBeam(form.getBeam());
        TbyEcologyReal check = this.getById(entity.getId());
        if (check != null) {
            entity.setUpdateTime(LocalDateTime.now());
            this.tbyEcologyRealRepo.updateById(entity, true);
        } else {
            entity.setCreateTime(LocalDateTime.now());
            this.tbyEcologyRealRepo.insert(entity, true);
        }
        return R.ok(entity);
    }

    @Override
    public TbyEcologyReal getById(Long id) {
        return this.tbyEcologyRealRepo.selectById(id);
    }
}
